from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import pandas as pd
from IPython.display import display
import matplotlib.pyplot as plt
import mglearn
from sklearn.neighbors import KNeighborsClassifier
import numpy as np


iris_dataset = load_iris()
x_train, x_test, y_train, y_test = train_test_split(iris_dataset['data'], iris_dataset['target'], random_state=0)

# iris_dataframe = pd.DataFrame(x_train, columns=iris_dataset.feature_names)
#
# grr = pd.plotting.scatter_matrix(iris_dataframe, c=y_train, figsize=(15, 15), marker='o', hist_kwds={'bins': 20}, s=60, alpha=.8, cmap=mglearn.cm3)

# plt.show()

# 构建算法
knn = KNeighborsClassifier(n_neighbors=1, algorithm='auto', leaf_size=30, metric='minkowski', metric_params=None, n_jobs=1, p=2, weights='uniform')
knn.fit(x_train, y_train)

# 我们将这朵花的测量数据转换为二维 NumPy 数组的一行，这是因为 scikit-learn的输入数据必须是二维数组。
X_new = np.array([[5, 2.9, 1, 0.2]])
# print("X_new.shape: {}".format(X_new.shape))

prediction = knn.predict(X_new)
# 根据我们模型的预测，这朵新的鸢尾花属于类别 0，也就是说它属于 setosa 品种。但我们
# 怎么知道能否相信这个模型呢？我们并不知道这个样本的实际品种，这也是我们构建模型
# 的重点啊！
# print("Prediction: {}".format(prediction))
# print("Predicted target name: {}".format(iris_dataset['target_names'][prediction]))

y_pred = knn.predict(x_test)
print("Test set predictions:\n {}".format(y_pred))

print("Test set score: {:.2f}".format(knn.score(x_test, y_test)))
